<%--
  Created by IntelliJ IDEA.
  User: HW
  Date: 2024/12/19
  Time: 下午7:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="com.data.ConnData" %>
<%
    request.setCharacterEncoding("utf-8");
    String us = request.getParameter("username");
    String ps = request.getParameter("password");

    ConnData objdata = new ConnData();
    try {
        Connection hello=objdata.getConn();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    String sql = "select * from user_table where nickname=? and password=?";
    PreparedStatement psmt = objdata.getPsmt(sql);
    try {
        psmt.setString(1, us);
        psmt.setString(2, ps);
        ResultSet rs = psmt.executeQuery();
        if (rs.next()) {
            session.setAttribute("sess_user", us);
            Cookie objuser = new Cookie("co_user", us);
            Cookie objpass = new Cookie("co_pass", ps);
            objuser.setMaxAge(24 * 60 * 60);
            objpass.setMaxAge(24 * 60 * 60);
            response.addCookie(objpass);
            response.addCookie(objuser);

            if (rs.getString("identity").equals("1")) {
                response.sendRedirect("administrator/admin.jsp");
            } else if (rs.getString("identity").equals("0")) {
                response.sendRedirect("user/conversation.jsp");
            }
        } else {
%>
登录不成功<br />
<a href="login.jsp">重新登录</a>
<%
    }
    rs.close();
    psmt.close();
    objdata.close();
} catch (SQLException e) {
    e.printStackTrace();
%>
数据库操作出现错误，请稍后再试！
<%
    }
%>